package com.houtai.controller.leading.figure;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.Page;
import com.houtai.bean.dto.leading.figure.TdLeadingFigureDetailDto;
import com.houtai.bean.vo.leading.figure.TdLeadingFigureDetailVo;
import com.houtai.bean.vo.leading.figure.TdLeadingFigureListVo;
import com.houtai.service.leading.figure.LeadingFigureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="领军人物接口", tags={"领军人物接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class LeadingFigureController {
    @Autowired
    private LeadingFigureService leadingFigureService;

    @ApiOperation(value="领军人物列表", notes="领军人物列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/leading/figure/list")
    public TdLeadingFigureListVo list (@ApiParam(value = "领军人物列表所需参数") @Valid @RequestBody Page dto, BindingResult result) {
        TdLeadingFigureListVo retInfo = new TdLeadingFigureListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = leadingFigureService.leadingFigureList(dto);
        }
        return retInfo;
    }

    @ApiOperation(value="领军人物详情", notes="领军人物详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/leading/figure/detail")
    public TdLeadingFigureDetailVo detail (@ApiParam(value = "领军人物详情所需参数") @Valid @RequestBody TdLeadingFigureDetailDto dto, BindingResult result) {
        TdLeadingFigureDetailVo retInfo = new TdLeadingFigureDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = leadingFigureService.detail(dto);
        }
        return retInfo;
    }
}
